<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndentController extends Controller
{
    public function index()
    {
        $data = DB::table('indent as r1') -> leftJoin('receiver as r2','r1.uaddress','=','r2.id') -> leftJoin('goods as r3','r1.goodsid','=','r3.id')
        -> select('r1.*','r2.phone','r2.address','r2.recname','r3.goodsname')
        -> paginate(15);
        foreach($data as $k => $v)
        {
            $v -> time = date("Y-m-d H:m:s",$v -> time);
        }
        return view('admin/indent/index') -> with(['title' => '订单列表','stitle' => '待发货','data' => $data]);
    }
    public function sendEdit($id)
    {
        $data = DB::table('indent')->where('id', $id)->first();
        if($data -> is_pay == 0)
        {
            return back() -> with(['info' => '用户未支付']);
        }
        if ($data -> is_send == 1)
        {
            return back() -> with(['info' => '已经发过货了']);
        }
        $res = DB::table('indent') -> where('id', $id) -> update(['is_send' => 1]);
        if($res)
        {
            $data = DB::table('indent as r1') -> leftJoin('receiver as r2','r1.uaddress','=','r2.id') -> leftJoin('goods as r3','r1.goodsid','=','r3.id')
                -> select('r1.*','r2.phone','r2.address','r2.recname','r3.goodsname')
                -> where('is_pay',1)
                -> where('is_send',0)
                -> paginate(15);
                foreach($data as $k => $v)
                {
                    $v -> time = date("Y-m-d H:i:s",$v -> time);
                }
            return view('/admin/indent/index') -> with(['title' => '订单列表','stitle' => '待发货订单','data' => $data,'info' => '发货成功']);
        }
    }
    public function is_pay()
    {
        $data = DB::table('indent as r1') -> leftJoin('receiver as r2','r1.uaddress','=','r2.id') -> leftJoin('goods as r3','r1.goodsid','=','r3.id')
        -> select('r1.*','r2.phone','r2.address','r2.recname','r3.goodsname')
        -> where('is_pay',0)
        -> paginate(15);
        foreach($data as $k => $v)
        {

            $v -> time = date("Y-m-d H:m:s",$v -> time);
        }
        return view('admin/indent/index') -> with(['title' => '订单列表','stitle' => '未支付订单','data' => $data]);
    }
    public function is_send()
    {
        $data = DB::table('indent as r1') -> leftJoin('receiver as r2','r1.uaddress','=','r2.id') -> leftJoin('goods as r3','r1.goodsid','=','r3.id')
        -> select('r1.*','r2.phone','r2.address','r2.recname','r3.goodsname')
        -> where('is_pay',1)
        -> where('is_send',0)
        -> paginate(15);
        foreach($data as $k => $v)
        {

            $v -> time = date("Y-m-d H:m:s",$v -> time);
        }
        return view('admin/indent/index') -> with(['title' => '订单列表','stitle' => '待发货订单','data' => $data]);
    }
    public function is_receive()
    {
        $data = DB::table('indent as r1') -> leftJoin('receiver as r2','r1.uaddress','=','r2.id') -> leftJoin('goods as r3','r1.goodsid','=','r3.id')
        -> select('r1.*','r2.phone','r2.address','r2.recname','r3.goodsname')
        -> where('is_pay',1)
        -> where('is_send',1)
        -> where('is_receive',0)
        -> paginate(15);
        foreach($data as $k => $v)
        {

            $v -> time = date("Y-m-d H:m:s",$v -> time);
        }
        return view('admin/indent/index') -> with(['title' => '订单列表','stitle' => '待收货订单','data' => $data]);
    }
    public function is_comment()
    {
        $data = DB::table('indent as r1') -> leftJoin('receiver as r2','r1.uaddress','=','r2.id') -> leftJoin('goods as r3','r1.goodsid','=','r3.id')
        -> select('r1.*','r2.phone','r2.address','r2.recname','r3.goodsname')
        -> where('is_pay',1)
        -> where('is_send',1)
        -> where('is_receive',1)
        -> where('is_comment',0)
        -> paginate(15);
        foreach($data as $k => $v)
        {

            $v -> time = date("Y-m-d H:m:s",$v -> time);
        }
        return view('admin/indent/index') -> with(['title' => '订单列表','stitle' => '待评价订单','data' => $data]);
    }
    public function finsh()
    {
        $data = DB::table('indent as r1') -> leftJoin('receiver as r2','r1.uaddress','=','r2.id') -> leftJoin('goods as r3','r1.goodsid','=','r3.id')
        -> select('r1.*','r2.phone','r2.address','r2.recname','r3.goodsname')
        -> where('is_pay',1)
        -> where('is_send',1)
        -> where('is_receive',1)
        -> where('is_comment',1)
        -> paginate(15);
        foreach($data as $k => $v)
        {

            $v -> time = date("Y-m-d H:m:s",$v -> time);
        }
        return view('admin/indent/index') -> with(['title' => '订单列表','stitle' => '已完成订单','data' => $data]);
    }
}
